package com.cql.sys.handler;

import com.cql.pojo.Role;
import com.cql.sys.service.RoleService;
import com.cql.utils.CommonResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {

    @Resource
    private RoleService roleService;

    @PostMapping("/add")
    public CommonResult addRole(@RequestBody Role role) {
        try {
            System.out.println("@@@"+role);
            int insert = roleService.insert(role);

            if (1 == insert) {
                return CommonResult.success(role);

            }
            if (0 == insert) {
                return CommonResult.error(201, "添加角色失败");
            }
            if (-1 == insert) {
                return CommonResult.error(-1, "角色已存在");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.error(500, "添加角色失败");
        }
        return null;
    }

    /*
    *  查询所有角色
    * @param pageNum
    * @param pageSize
    * */
    @GetMapping("/roles/{pageNum}/{pageSize}")
    public CommonResult<PageInfo<Role>> roles(@PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize) {
        try {
            PageHelper.startPage(pageNum, pageSize);
            List<Role> roles = roleService.roles();
            PageInfo<Role> pageInfo = new PageInfo<>(roles);
            return  null != roles ? CommonResult.success(pageInfo) : CommonResult.error(201, "请求成功,没有查询到该数据");

        } catch (Exception e) {
            return CommonResult.error(500, "系统异常,请联系管理员");
        }
    }

    @GetMapping("/find/{name}")
    public CommonResult<Role> findRoleByName(@PathVariable("name") String name) {
        try {
            System.out.println("@@@"+name);
            Role role = roleService.findByName(name);
            return null != role ? CommonResult.success(role) : CommonResult.error(201, "请求成功,没有查询到该数据");
        } catch (Exception e) {
            return CommonResult.error(500, "系统异常,请联系管理员");
        }
    }

    @PostMapping("/update")
    public CommonResult updateRole(@RequestBody Role role) {
        try {
            int update = roleService.update(role);
            System.out.println("@@@"+update);
            return 1 == update ? CommonResult.success(role) : CommonResult.error(201, "更新角色失败");
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.error(500, "系统异常,请联系管理员");
        }
    }

    @DeleteMapping("/delete/{rid}")
    public CommonResult deleteRole(@PathVariable("rid") Integer rid) {
        try {
            Role role = new Role();
            role.setRid(rid);
            int delete = roleService.delete(role);
            return 1 == delete ? CommonResult.success(rid) : CommonResult.error(201, "删除角色失败");
        } catch (Exception e) {
            return CommonResult.error(500, "系统异常,请联系管理员");
        }
    }
}
